﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Class_KetNoi;

namespace GiaoDien
{
    public partial class uc_LuongNhanVien : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_LuongNhanVien()
        {
            InitializeComponent();
        }
        KetNoi_BangLuong kn = new KetNoi_BangLuong();
        public void LoadDuLieu()
        {

            gridControl1.DataSource = kn.Load_BangLuong();
        }
        public void Nut()
        {
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            btnHuy.Enabled = true;
        }
        private void uc_LuongNhanVien_Load(object sender, EventArgs e)
        {
            kn.connect();
            LoadDuLieu();
            lbMaLuong.Hide();
            txtMaLuong.Hide();
            btnHuy.Enabled = false;
            btnLuu.Enabled = false;
        }
        int flag;
        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 0;
            Nut();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 1;
            Nut();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            kn.xoa_BangLuong(txtMaLuong.Text );
            LoadDuLieu();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (cobThang.Text != "" && mskNam.Text != "")
            {
                if (flag == 0)
                {
                    kn.them_BangLuong(cobThang.Text, mskNam.Text, txtMaNV.Text, cobMaPB.Text, txtLuongCb.Text, txtNgayTangCa.Text, txtLuongTangCa.Text, txtTienBH.Text, txtThucLanh.Text);
                    LoadDuLieu();
                    if (MessageBox.Show("Bạn có muốn in luong nhân viên ko?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        gridControl1.ShowPrintPreview();
                }
                if (flag == 1)
                {
                    kn.sua_BangLuong(txtMaLuong.Text, cobThang.Text, mskNam.Text, txtMaNV.Text, cobMaPB.Text, txtLuongCb.Text, txtNgayTangCa.Text, txtLuongTangCa.Text, txtTienBH.Text, txtThucLanh.Text);
                }
            }
            if (cobThang.Text == "" || mskNam.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập đủ dữ liệu!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                mskNam.Focus();
            }
            LoadDuLieu();
            btnThem.Enabled = true;
            btnSua.Enabled =true;
            btnXoa.Enabled =true;

            btnLuu.Enabled = false;
            btnHuy.Enabled = false;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {// tùy theo sự kiện Load cuả form load mà làm có dâu hay ko có dấu
            txtMaLuong.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Lương").ToString();
            cobThang.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Tháng").ToString();
            mskNam.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Năm").ToString();
            txtMaNV.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhân Viên").ToString();
            cobMaPB.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Phòng Ban").ToString();
            txtLuongCb.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Lương Cơ Bản").ToString();
            txtNgayTangCa.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Số Ngày Tăng Ca").ToString();
            txtLuongTangCa.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Lương Tăng Ca").ToString();
            txtTienBH.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Tiền Bảo Hiểm").ToString();
            txtThucLanh.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Thực Lãnh").ToString();
        }

        private void txtMaNV_TextChanged(object sender, EventArgs e)
        {
            string s = "select * from tbl_NhanVien where MaNV='"+txtMaNV.Text +"' ";
            DataTable dt = new DataTable();
            try
            {
                dt = kn.taobang(s);
            }
            catch (Exception)
            {
 
            }
            if (dt.Rows.Count != 0)
            {

                    errorProvider1.Clear();
                    cobMaPB.DataSource = kn.taobang(s);
                    cobMaPB.DisplayMember = "MaPB";
            }
            else
            {
                errorProvider1.SetError(txtMaNV, "Mã Nhân Viên Không Tồn Tại!");
                txtMaNV.Focus();
            }
        }

        private void cobMaPB_TextChanged(object sender, EventArgs e)
        {
            string sql = "select * from tbl_NhanVien,tbl_ChucVu where tbl_NhanVien.MaCV=tbl_ChucVu.MaCV and  tbl_NhanVien.MaNV='" + txtMaNV.Text  + "'";
            txtLuongCb.DataSource = kn.taobang(sql);
            txtLuongCb.DisplayMember = "LuongCoBan";
        }

        private void txtTienBH_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtTienBH_TextChanged(object sender, EventArgs e)
        {
            if (txtNgayTangCa.Text != "")
            {
                try
                {
                    errorProvider1.Clear();
                    float lcb = 0.0f, luongtangca = 0.0f, tienbh = 0.0f;
                    int ngaytangca;
                    lcb = float.Parse(txtLuongCb.Text);
                    ngaytangca = int.Parse(txtNgayTangCa.Text);
                    luongtangca = float.Parse(txtLuongTangCa.Text);
                    tienbh = float.Parse(txtTienBH.Text);
                    txtThucLanh.Text = (lcb + (ngaytangca * luongtangca) + tienbh).ToString();
                }
                catch (Exception)
                { 
                }
            }
            else
                errorProvider1.SetError(txtNgayTangCa, "Bạn Chưa Chọn Số Ngày tăng ca!");
        }

        private void txtLuongTangCa_TextChanged(object sender, EventArgs e)
        {
            txtTienBH_TextChanged(sender,e);
        }

        private void txtNgayTangCa_TextChanged(object sender, EventArgs e)
        {
           txtTienBH_TextChanged(sender,e);
        }

        private void txtLuongCb_TextChanged(object sender, EventArgs e)
        {
            txtTienBH_TextChanged(sender,e);
        }

        private void txtTienBH_Click(object sender, EventArgs e)
        {
            txtTienBH.SelectAll();
        }

        private void txtLuongTangCa_Click(object sender, EventArgs e)
        {
            txtLuongTangCa.SelectAll();
        }

        private void txtLuongCb_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
